package net.sk.china.platform.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sk.china.platform.model.Category;
import net.sk.china.platform.request.category.CategoryRequest;
import net.sk.china.platform.request.category.CategorySearchRequest;
import net.sk.china.platform.response.CategoryTreeResponse;
import net.sk.china.platform.service.CategoryService;
import net.sk.china.common.exception.TalkException;
import net.sk.china.common.request.PageParamRequest;
import net.sk.china.common.response.CommonPage;
import net.sk.china.common.response.CommonResult;
import net.sk.china.common.utils.TalkUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * +----------------------------------------------------------------------
 * // | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * // +----------------------------------------------------------------------
 * // | Copyright (c) 2018-2024 聊来 All rights reserved.
 * // +----------------------------------------------------------------------
 * // | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * // +----------------------------------------------------------------------
 * // | @Author: 聊来 <18970881148@qq.com>
 * // +----------------------------------------------------------------------
 * // | DateTime: 2024/6/2 14:27
 * // +----------------------------------------------------------------------
 */

@RestController
@RequestMapping("category/")
@Api(tags = "分类服务")
public class CategoryController {

    private final CategoryService categoryService;

    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }


    @PreAuthorize("hasAuthority('admin:category:list')")
    @ApiOperation(value = "分页分类列表")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public CommonResult<CommonPage<Category>>  getList(@ModelAttribute CategorySearchRequest request, @ModelAttribute PageParamRequest pageParamRequest) {
        CommonPage<Category> categoryCommonPage = CommonPage.restPage(categoryService.getList(request, pageParamRequest));
        return CommonResult.success(categoryCommonPage);
    }

    @PreAuthorize("hasAuthority('admin:category:save')")
    @ApiOperation(value = "新增")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public CommonResult<String> save(@Validated CategoryRequest categoryRequest) {
        if (categoryService.create(categoryRequest)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }


    @PreAuthorize("hasAuthority('admin:category:delete')")
    @ApiOperation(value = "删除")
    @RequestMapping(value = "delete", method = RequestMethod.GET)
    @ApiImplicitParam(name="id", value="分类ID", dataTypeClass = Integer.class)
    public CommonResult<String> delete(@RequestParam(value = "id") Integer id) {
        if (categoryService.delete(id) > 0) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @PreAuthorize("hasAuthority('admin:category:update')")
    @ApiOperation(value = "修改")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ApiImplicitParam(name="id", value="分类ID", dataTypeClass = Integer.class)
    public CommonResult<String> update(@RequestParam Integer id, @ModelAttribute CategoryRequest categoryRequest) {
        if (null == id || id <= 0) {
            throw new TalkException("id 参数不合法");
        }
        categoryRequest.setExtra(categoryRequest.getExtra());
        if (categoryService.update(categoryRequest, id)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @PreAuthorize("hasAuthority('admin:category:info')")
    @ApiOperation(value = "分类详情")
    @RequestMapping(value = "info", method = RequestMethod.GET)
    @ApiImplicitParam(name="id", value="分类ID", dataTypeClass = Integer.class)
    public CommonResult<Category> info(@RequestParam(value = "id") Integer id) {
        Category category = categoryService.getById(id);
        return CommonResult.success(category);
    }


    @PreAuthorize("hasAuthority('admin:category:list:tree')")
    @ApiOperation(value = "配置分类")
    @RequestMapping(value = "list/tree", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name="type", value="类型ID | 类型，1 产品分类，2 附件分类，3 文章分类， 4 设置分类， 5 菜单分类， 6 配置分类， 7 秒杀配置", example = "1", dataTypeClass = Integer.class),
            @ApiImplicitParam(name="status", value="-1=全部，0=未生效，1=已生效", example = "1", dataTypeClass = Integer.class),
            @ApiImplicitParam(name="name", value="模糊搜索", example = "电视", dataTypeClass = String.class)
    })
    public CommonResult<List<CategoryTreeResponse>> getListTree(@RequestParam(name = "type") Integer type,
                                                                @RequestParam(name = "status") Integer status,
                                                                @RequestParam(name = "name", required = false) String name) {
        List<CategoryTreeResponse> listTree = categoryService.getListTree(type,status,name);
        return CommonResult.success(listTree);
    }

    @PreAuthorize("hasAuthority('admin:category:list:ids')")
    @ApiOperation(value = "根据id集合获取分类列表")
    @RequestMapping(value = "list/ids", method = RequestMethod.GET)
    @ApiImplicitParam(name = "ids", value="分类id集合", dataTypeClass = String.class)
    public CommonResult<List<Category>> getByIds(@Validated @RequestParam(name = "ids") String ids) {
        return CommonResult.success(categoryService.getByIds(TalkUtil.stringToArray(ids)));
    }

    @PreAuthorize("hasAuthority('admin:category:update:status')")
    @ApiOperation(value = "更改分类状态")
    @RequestMapping(value = "updateStatus/{id}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "id", value="分类id", dataTypeClass = Integer.class)
    public CommonResult<Object> getByIds(@Validated @PathVariable(name = "id") Integer id) {
        if (categoryService.updateStatus(id)) {
            return CommonResult.success("修改成功");
        } else {
            return CommonResult.failed("修改失败");
        }
    }
}
